﻿using B.S.BasicData.Api.Read.Application.Command.ShenPi;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domian.Entities.shenpi;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using NLog.Time;
using System.Xml.Linq;

namespace B.S.BasicData.Api.Read.Application.Handler.ShenPi
{
    public class ShenpiGetHandler : IRequestHandler<ShenpiGetCommand, ApiPaging<ShenpiGetDTO>>
    {
        private readonly IBaseRepository<LeaveModel> leaveRepo;
        private readonly IBaseRepository<DeptModel> deptRepo;

        public ShenpiGetHandler(IBaseRepository<LeaveModel> leaveRepo, IBaseRepository<DeptModel> deptRepo)
        {
            this.leaveRepo = leaveRepo;
            this.deptRepo = deptRepo;
        }
        public Task<ApiPaging<ShenpiGetDTO>> Handle(ShenpiGetCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPaging<ShenpiGetDTO>();
            var leave = leaveRepo.GetAll().Where(x => x.IsDel == false);
            var dept = deptRepo.GetAll().Where(x => x.IsDel == false);
            if (!string.IsNullOrEmpty(request.Name))
            {
                leave = leave.Where(x => x.Name.Contains(request.Name));
            }
            if (request.BeginTime != null)
            {
                leave = leave.Where(x => x.BeginTime <= request.BeginTime);
            }
            if (request.OverTime != null)
            {
                leave = leave.Where(x => x.OverTime >= request.OverTime);
            }
            if (request.ShenpiState != null)
            {
                leave = leave.Where(x => x.ShenpiState==request.ShenpiState);
            }
            var linq = from a in leave
                       join b in dept on a.Bid equals b.Bid
                       select new ShenpiGetDTO
                       {
                           Id = a.Id,
                           Bid = a.Bid,
                           DeptName = b.DeptName,
                           Name = a.Name,
                           LeaveType = a.LeaveType,
                           BeginTime = a.BeginTime,
                           OverTime = a.OverTime,
                           CountTime = a.CountTime,
                           BaoDaoTime = a.BaoDaoTime,
                           LeaveReason = a.LeaveReason,
                           ShenpiReason = a.ShenpiReason,
                           Deptlingdao = a.Deptlingdao,
                           ShenpiState = a.ShenpiState,
                           CreateTime = a.CreateTime,
                       };
            var totalcount = linq.Count();
            var totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
            linq = linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            res.totalcount = totalcount;
            res.totalpage = totalpage;
            res.Message = "显示成功";
            res.Data = linq.ToList();
            res.Code = ApiEnum.Ok;
            return Task.FromResult(res);
        }
    }
}
